#include "ThreadPool.h"
bool isEmptyDir(int id,MYSQL* mysql){

    char sql[4096];
    bzero(sql,sizeof(sql));
    sprintf(sql,"select id from virtual_file where tomb != 1 and pre_id = %d;",id);
    int qret = mysql_query(mysql,sql);
    MYSQL_QUERY_CHECK(qret,mysql);
    MYSQL_RES *res = mysql_store_result(mysql);
    printf("total rows = %lu\n",mysql_num_rows(res));
    if((int)mysql_num_rows(res) == 0)
        return true;//表示目录为空
    else
        return false;//表示目录非空
}
int rmdirFunc(int id_now,const char *arg,const char* user,MYSQL *mysql){
    int retval=0;
    char argcpy[1024];
    char* path[1024];
    strcpy(argcpy,arg);
    char* token;
    token=strtok(argcpy," ");
    int idx=0;
    path[idx++]=token;
    while((path[idx++]=strtok(NULL," "))!=NULL);
    for(int i=0;i<idx && path[i]!=NULL;i++){
        int id=getAbsolutePath(id_now,path[i],user,mysql);
        if(id==-1){
            printf("文件夹不存在，不可删除\n");
            retval=-1;
            break;
        }
        if(isTomb(id,mysql)){
            retval=-1;
            printf("路径包含已被删除的元素，错误\n");
            break;
        }
        if(!isDir(id,mysql)){
            retval=-1;
            printf("将要删除的非文件夹，错误\n");
            break;
        }
        if(!isEmptyDir(id,mysql)){
            retval=-1;
            printf("所要删除文件夹非空，不可删除\n");
            break;
        }

        char sql[4096];
        bzero(sql,sizeof(sql));
        sprintf(sql,"update virtual_file set tomb = 1 where id =%d;",id);

       // printf("%s\n",sql);
        int qret = mysql_query(mysql,sql);
        MYSQL_QUERY_CHECK(qret,mysql);
    }
    return retval;
}
